<?php

namespace controller;

/**
 * Kontroler do obsługi producentów sklepu
 *
 * @author Damian
 */
class ShopAdmin_Manufacturers extends Seller {

    public function index() {
        $pictureCriteria = new \model\Criteria();
        $pictureCriteria->from(\model\TblPicture::getModel()->getTable())
                ->where(\model\TblPicture::SHOP_ID, "=",  $this->shop->ShopID)
                ->where(\model\TblPicture::DELETED, "=", 0,false, " and ");
        $files = \model\TblPicture::getModel()->all($pictureCriteria);
        $filesDataTable = new \model\DataTable($files,true,\model\TblPicture::NAME);
        $layoutView = $this->getLayoutView();
        $layoutView->set("paneltitle", "Lista producentów");
        $Manufacturers = \model\TblManufacturer::getModel()->getAllByShopID($this->shop->ShopID);
        $modelTable = new \model\DataTable($Manufacturers);
        $ManufacturersData = $modelTable->getModelData();
        $view = $this->getActionView();
        $NumberProducts=array();
        $NumberVisableProducts=array();
        foreach ($ManufacturersData as $Manufacturer) {
            $NumberProducts[$Manufacturer["ManufacturerID"]] = \model\TblProduct::getModel()->createCriteria($this->shop->ShopID)
                            ->where(\model\TblProduct::MANUFACTURER_ID, "=", $Manufacturer["ManufacturerID"], \model\TblManufacturer::getModel()->getTable(), " and ")->count();
            $NumberVisableProducts[$Manufacturer["ManufacturerID"]] = \model\TblProduct::getModel()->createCriteria($this->shop->ShopID)
                            ->where(\model\TblProduct::ACTIVE, "=", 1, false, " and ")
                    ->where(\model\TblProduct::MANUFACTURER_ID, "=", $Manufacturer["ManufacturerID"], \model\TblManufacturer::getModel()->getTable(), " and ")->count();
        }
        $view->set("NumberProducts", $NumberProducts);
        $view->set("NumberVisableProducts", $NumberVisableProducts);
        if ($Manufacturers) {
            $view->set("Manufacturers", $ManufacturersData);
        }
        $name = \library\RequestMethods::post("inputShopManufacturerName");
        if ($name != null) {
            $Manufacturer = new \model\TblManufacturer([
                \model\TblManufacturer::NAME => $name,
                \model\TblManufacturer::SHOP_ID => $this->shop->ShopID
            ]);
            $Manufacturer->generateURL();
            if ($Manufacturer->validate()) {
                try {
                    if (file_exists("img/".substr($this->shop->URL,0,strlen($this->shop->URL)-1)."/128px/".$Manufacturer->Name.".jpg")){
                        $modelData = $filesDataTable->getModelData(true);
                        $Manufacturer->PictureID=$modelData[$Manufacturer->Name.".jpg"]->PictureID;
                    }
                    if (file_exists("img/".substr($this->shop->URL,0,strlen($this->shop->URL)-1)."/128px/".$Manufacturer->Name.".png")){
                        $modelData = $filesDataTable->getModelData(true);
                        $Manufacturer->PictureID=$modelData[$Manufacturer->Name.".png"]->PictureID;
                    }
                    $Manufacturer->save();
                    $this->redirect($this->shop->URL . "/ShopAdmin_Manufacturers");
                } catch (\model\exceptions\ExceptionSQLQUery $ex) {
                    if ($ex->getCode() == 1062) {
                        $errors = array();
                        $errors["Name"][] = "Producent o takiej nazwie już istnieje";
                        $view = $this->getActionView();
                        $view->set("errors", $errors);
                        $this->render();
                    } else {
                        throw $ex;
                    }
                }
            } else {
                $view = $this->getActionView();
                $view->set("errors", $Manufacturer->getErrors());
                $this->render();
            }
        }
        $this->render();
    }

}
